package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspWriter;

import Model.Approval;

/**
 * Servlet implementation class StaffappStaffSidebox
 */
@WebServlet("/StaffappStaffSidebox")
public class StaffappStaffSidebox extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public StaffappStaffSidebox() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		if (request.getParameter("staffid") != "") {
			String staffid = request.getParameter("staffid");
			String appointmentid = request.getParameter("appointmentid");
			//System.out.println(appointmentid);
			int no = Database.StaffAppointmentDB.numberApproved(staffid);
			ArrayList<Approval> getMyApprovedAppointments = Database.StaffAppointmentDB
					.getMyApprovedAppointments(staffid);
			ArrayList<Approval> getMyPendingAppointments = Database.StaffAppointmentDB
					.getMyPendingAppointments(staffid);
			out.println("<a href=\"#\" onclick=\"document.getElementById('staffdetails').style.display = 'none';\" style=\"float:right;\">X</a>");
	
			// Validation
			if (getMyApprovedAppointments.size() < 6 && no >= 6) {
				// Staff may be appointed more appointments than intended
				out.println("<b>No. of Appointments : </b><font color=\"yellow\">"
						+ no + "</font><br/><br/>");
			} else if (getMyApprovedAppointments.size() == 6) {
				// If the staff has 6 approved
				// Staff is appointed to max amount of appointments!
				out.println("<b>No. of Appointments : </b><font color=\"red\">"
						+ no + "&nbsp;&nbsp;</font><img src=\" images/warning.png \" title=\"Staff is being appointed to more appointments than stipulated \" /><br/><br/>");
			} else {
				out.println("<b>No. of Appointments : </b>" + no + "<br/><br/>");
			}
			out.println("<b>Approved Appointment List : </b><br/>");
			if (getMyApprovedAppointments.size() == 0) {
				out.println("-<br/>");
			} else {
				for (int i = 0; i < getMyApprovedAppointments.size(); i++) {
					
					if(appointmentid.compareToIgnoreCase(getMyApprovedAppointments.get(i).getAppointmentID()) == 0){
						
						out.println("<font color=\"red\">" + getMyApprovedAppointments.get(i)
								.getAppointment() + "&nbsp;&nbsp;</font><img src=\" images/error.gif \" title=\"Staff is already appointed to this appointment!\"/><br/>");
					}
					else{
						out.println(getMyApprovedAppointments.get(i)
								.getAppointment() + "<br/>");
					}
					
				}
			}
			
			//Logic to display pending appointments
			out.println("<b>Pending Appointment List : </b><br/>");
			if (getMyPendingAppointments.size() == 0) {
				out.println("-<br/>");
			} else {
				for (int i = 0; i < getMyPendingAppointments.size(); i++) {
					
					if(appointmentid.compareToIgnoreCase(getMyPendingAppointments.get(i).getAppointmentID()) == 0){
						out.println("<font color=\"red\">" + getMyPendingAppointments.get(i)
								.getAppointment() + "&nbsp;&nbsp;</font><img src=\" images/error.gif \" title=\"Staff is already appointed to this appointment!\"/><br/>");
					}
					else{
					out.println(getMyPendingAppointments.get(i)
							.getAppointment() + "<br/>");
					}
				}
			}

		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		
	}

}
